package com.xieteng.controller;

import com.xieteng.entity.Employee;
import com.xieteng.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Value("${photo.dir}")
    private String realPath;

    //展示员工信息
    @GetMapping("/findAll")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    //保存员工信息
    @PostMapping("/save")
    public Map<String, Object> save(Employee employee, MultipartFile photo) throws IOException {
        log.info("员工信息：[{}]", employee.toString());
        log.info("员工头像信息：[{}]", photo.getOriginalFilename());
        Map<String, Object> map = new HashMap<>();
        try {
            //头像保存
            String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath, newFileName));
            //设置头像地址
            employee.setPath(newFileName);
            //保存员工信息
            employeeService.save(employee);
            map.put("status", true);
            map.put("msg", "员工信息保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", false);
            map.put("msg", "员工信息保存失败!");
        }
        return map;
    }

    //删除员工信息
    @GetMapping("/delete")
    public Map<String, Object> delete(int id) {
        Map<String, Object> map = new HashMap<>();
        log.info("删除用户的id：[{}]", id);
        try {
            //删除上传的图片信息
            Employee employee = employeeService.findEmployee(id);
            File file = new File(realPath, employee.getPath());
            if (file.exists()) {
                file.delete();
            }
            //删除数据库里的员工信息
            employeeService.delete(id);
            map.put("status", true);
            map.put("msg", "删除用户成功！");
        } catch (Exception e) {
            map.put("status", true);
            map.put("msg", "删除用户失败！");
        }
        return map;
    }

    //查询单个员工的信息
    @GetMapping("/findOneEmployee")
    public Employee findOneEmployee(int id) {
        log.info("查询单个员工的id：[{}]", id);
        return employeeService.findEmployee(id);
    }

    //更新单个员工的信息
    @PostMapping("/updateEmployee")
    public Map<String, Object> updateEmployee(Employee employee, MultipartFile photo) {
        Map<String, Object> map = new HashMap<>();
        log.info("员工信息：[{}]", employee.toString());
        try {
            if (photo != null && photo.getSize() != 0) {
                log.info("员工头像信息：[{}]", photo.getOriginalFilename());
                //头像保存
                String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath, newFileName));
                //设置头像地址
                employee.setPath(newFileName);
            }
            //保存员工信息
            employeeService.update(employee);
            map.put("status", true);
            map.put("msg", "员工信息保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", false);
            map.put("msg", "员工信息保存失败!");
        }
        return map;
    }
}
